<?php
class Place_PlaceEditController extends Zend_Controller_Action{
    
    public function init(){
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        $userSession = new Zend_Session_Namespace('user_session');
        $userSession->setExpirationSeconds($constants->SESSION_TIMEOUT);
        $user = $userSession->user;
        $userId = $user->sale_user_id;
        try{
            if(isset($userId)){
                // $userRoleModel = new Admin_Model_SaleUserRole();
                // $userRole = $userRoleModel->getRoleOfUser($userId);
//                 
                // $saleUserRoleFeature = new Admin_Model_SaleUserRoleFeature();
                // $featureArr = $saleUserRoleFeature->getFeatureOfUserRole($userRole);
                // $featureIdArr = array();
                // foreach ($featureArr as $key => $value) {
                    // array_push($featureIdArr,$value->sale_feature_id);
                // }
                // if(!in_array($constants->FEATURE_APPROVE_DEAL, $featureIdArr)){
                    // $this->_redirect('/admin/index');
                // }
                $this->view->title = "Approve Deal";    
                $this->view->userName = $user->full_name;
                
            }else{
                $this->_redirect('/place/index/index');
            }   
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }
    
    public function indexAction(){
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        
        try{
            if(!isset($_GET['place_id'])){
                throw new Exception($constants->ERROR_STRING);
            }
            
            $placeId = $_GET['place_id'];
            $merchantPlaceModel = new Place_Model_MerchantPlace();
            $place = $merchantPlaceModel->getPlaceDetail($placeId);
            
            $userSession = new Zend_Session_Namespace('user_session');
            $user = $userSession->user;
            $userId = $user->sale_user_id;
            
            $userRoleModel = new Place_Model_SaleUserRole();
            $userRole = $userRoleModel->getRoleOfUser($userId);
            
            $saleUserRoleFeature = new Place_Model_SaleUserRoleFeature();
            $featureArr = $saleUserRoleFeature->getFeatureOfUserRole($userRole);
            $featureIdArr = array();
            foreach ($featureArr as $key => $value) {
                array_push($featureIdArr,$value->sale_feature_id);
            }
            if(!in_array($constants->FEATURE_CHECK_PLACE, $featureIdArr)){
                $this->_redirect('/place/index/index');
            }
            
            $tagCategory = array('Active life' => "Active life",
                                'ATM' => 'ATM',
                                'Beauty & Spa' => 'Beauty & Spa',
                                'Café' => 'Café',
                                'Education' => 'Education',
                                'Entertainment' => 'Entertainment',
                                'Finance' => 'Finance',
                                'Fuel station' => 'Fuel station',
                                'Health care' => 'Health care',
                                'Hotel & Accommodation' => 'Hotel & Accommodation',
                                'Nightlife' => 'Nightlife',
                                'Organizations' => 'Organizations',
                                'Restaurants' => 'Restaurants',
                                'Services' => 'Services',
                                'Shopping' => 'Shopping',
                                'Sightseeings' => 'Sightseeings',
                                'Street food & beverages' => 'Street food & beverages',
                                'Tourism & Travel' => 'Tourism & Travel',
                                'Transport' => 'Transport');
            
            $this -> view -> userFeature = $featureArr;
            $this->view->place = $place;
            $this->view->placeId = $placeId;
            $this->view->tagCategory = $tagCategory;
            
            
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }

    public function editAction(){
        $this -> _helper -> viewRenderer -> setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        $DB -> beginTransaction();
        
        try{
            
            $userSession = new Zend_Session_Namespace('user_session');
            $user = $userSession->user;
            $userId = $user->sale_user_id;
            
            if(!isset($_POST['place_id']) || !isset($_POST['latitude']) || 
                !isset($_POST['longitude']) || !isset($_POST['name']) || 
                !isset($_POST['address']) || !isset($_POST['product_service']) ){
                throw new Exception($constants->ERROR_STRING);
            }
            
            $place_id = $_POST['place_id'];
            $latitude = $_POST['latitude'];
            $longitude = $_POST['longitude'];
            $name = $_POST['name'];
            $address = $_POST['address'];
            $product_service = $_POST['product_service'];
            $phone = $_POST['phone'];
            $mail = $_POST['mail'];
            $website = $_POST['website'];
            $description = $_POST['description'];
            $tag = $_POST['tag'];
            
            if( $latitude == '' || $longitude == '' ||
                $latitude == 0 || $longitude == 0 ||
                $name == '' || $address == '' || 
                $product_service == '' || $tag == ''){
                    throw new Exception($constants->MISSING_MERCHANT_INFO);
            }
            
            $arrParams = array("place_id"=>$place_id,
                            "latitude"=>$latitude,
                            "longitude"=>$longitude,
                            "name"=>$name,
                            "address"=>$address,
                            "phone"=>$phone,
                            "mail"=>$mail,
                            "website"=>$website,
                            "product_service"=>$product_service,
                            "description"=>$description,
                            "tag"=>$tag, 
                            "edit_status"=>$constants->PLACE_EDITED_STATUS, 
                            "sale_user_id"=>$userId,
                            "status"=>$constants->APPROVE_REQUEST);
            
            
            $merchantPlaceModel = new Place_Model_MerchantPlace();
            $merchantPlaceModel->edit($arrParams);
            $DB -> commit();
            
            echo $constants -> UPDATE_MERCHANT_SUCCESS;
            
        }catch(Exception $e){
            $DB->rollback();
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }
}
